﻿<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>房间内有 100 人，每人有 100 块，每分钟随机给另一个人 1 块，最后这个房间内的财富分布怎样？</title>

    <script type="text/javascript" src="/Scripts/require.js"></script>
    <script type="text/javascript" src="/Scripts/route.js"></script>
    <script type="text/javascript">
        require([
            ToResourceURL(1, "/Scripts/jquery-2.2.4.min.js"),
            ToResourceURL(1, "/Scripts/echarts.5.0.2.min.js"),
        ], function (r1, echarts) {
            load_exe(echarts);
        });

        function load_exe(echarts) {
            var pserson_count = 100;
            var start_sum_money = 100;
            var single_money = 1;

            var source = [];
            for (var i = 0; i < pserson_count; i++) {
                source.push([i.toString(), start_sum_money]);
            }
            var count = 0;
            var option = {
                dataset: [
                    {
                        dimensions: ['name', 'money'],
                        source: source,
                    },
                    {
                        transform: {
                            type: 'sort',
                            config: { dimension: 'money', order: 'asc' }
                        }
                    }
                ],
                xAxis: {
                    type: 'category',
                    axisLabel: { interval: 0, rotate: 30 }
                },
                yAxis: {},
                series: {
                    type: 'bar',
                    encode: {
                        x: 'name',
                        y: 'money'
                    },
                    datasetIndex: 1
                }
            };
            var chartDom = document.getElementById('main');
            var myChart = echarts.init(chartDom);
            myChart.setOption(option);
            function update_chart() {
                option.dataset.source = source;
                myChart.setOption(option);
            }
            function move_number() {
                for (var i = 0; i < source.length; i++) {
                    source[i][1] -= single_money;
                    give_index = Math.floor(Math.random() * pserson_count);
                    source[give_index][1] += single_money;
                }
            }
            function update_page() {
                document.getElementById('exe_count').innerHTML = count;
            }
            setInterval(function () {
                count++;
                move_number();
                update_page();
                if (count % 8 === 0) {
                    update_chart();
                }
            }, 60);
        };
    </script>
</head>
<body>
    <div>
        <span>房间内有 100 人，每人有 100 块，每分钟随机给另一个人 1 块，最后这个房间内的财富分布怎样？</span>
        <br />
        <span>执行次数: </span><span id="exe_count">0</span>
    </div>
    <div>
        <style type="text/css">
            #main {
                width: 100%;
                height: 600px;
            }
        </style>
        <div id="main"></div>
    </div>
</body>
</html>
